﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADC.BL;
using ADC.Utilites;

namespace ADC.Web.Admin
{
    public partial class ManagePromotion : System.Web.UI.Page
    {
        log4net.ILog logger = log4net.LogManager.GetLogger("ManagePromotion");
        private int _mode;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitData();

            }
        }

        private void InitData()
        {
            // Check if the mode is existing
            if (Request.Params["mode"] != null)
            {
                try
                {
                    _mode = Convert.ToInt32(Request.Params["mode"].ToString());

                    if (_mode.Equals(1)) // Edit Mode
                    {
                        lblHeader.Text = "Cận nhật khuyến mại";
                        LinkButtonAddNew.Text = "Cập nhật";
                        if (Request.Params["ID"] != null)
                        {
                            var ID = Convert.ToInt32(Request.Params["ID"].ToString());
                            var consulting = PromotionFactory.GetByID(ID);

                            txtName.Text = consulting.Title;
                            txtBody.TextValue = consulting.Body;
                            txtBodyDetail.TextValue = consulting.BodyDetails;

                            if (consulting.IsVisibled != null)
                            {
                                CheckBoxIsVisible.Checked = Convert.ToBoolean(consulting.IsVisibled);
                            }
                            else CheckBoxIsVisible.Checked = false;

                            ImageConsulting.ImageUrl = "~/" + consulting.ImagePath;
                        }
                    }
                    else // New mode
                    {
                        lblHeader.Text = "Thêm thông tin khuyến mại";
                        LinkButtonAddNew.Text = "Thêm mới";
                    }

                }
                catch (Exception ex)
                {
                    logger.Error(ex.ToString());
                    if (GlobalInfo.IsEnableDebugMode) throw;
                }
            }

        }

        protected void LinkButtonAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (Request.Params["mode"] == null) return;
                _mode = Convert.ToInt32(Request.Params["mode"]);
                if (_mode.Equals(1)) // Edit 
                {
                    if (Request.Params["ID"] != null)
                    {
                        var ID = Convert.ToInt32(Request.Params["ID"].ToString());

                        var promotion = PromotionFactory.GetByID(ID);
                        if (promotion == null) return;
                        promotion.Title = txtName.Text;
                        promotion.Body = txtBody.TextValue;
                        promotion.BodyDetails = txtBodyDetail.TextValue;
                        promotion.IsVisibled = CheckBoxIsVisible.Checked;
                        promotion.ModifiedBy = promotion.CreatedBy = ((DAL.User)Session["UserInfo"]).UserID;
                        //Upload the file 

                        if (FileUploadConsulting.HasFile)
                        {
                            FileUploadConsulting.SaveAs(Request.PhysicalApplicationPath + Utilites.Common.IMAGE_PROMOTION_PATH + FileUploadConsulting.FileName);
                            promotion.ImagePath = Utilites.Common.IMAGE_PROMOTION_PATH + FileUploadConsulting.FileName;

                        }

                        PromotionFactory.Update(promotion);

                        Response.Redirect("ListPromotion.aspx", false);
                    }
                }
                else // New mode
                {
                    var promotion = new DAL.Promotion();

                    promotion.Title = txtName.Text;
                    promotion.Body = txtBody.TextValue;
                    promotion.BodyDetails = txtBodyDetail.TextValue;
                    promotion.IsVisibled = CheckBoxIsVisible.Checked;
                    promotion.CreatedDate = DateTime.Now;
                    promotion.CreatedBy = ((DAL.User)Session["UserInfo"]).UserID;
                    promotion.ModifiedDate = DateTime.Now;

                    if (FileUploadConsulting.HasFile)
                    {
                        FileUploadConsulting.SaveAs(Request.PhysicalApplicationPath + Utilites.Common.IMAGE_PROMOTION_PATH + FileUploadConsulting.FileName);
                        promotion.ImagePath = Utilites.Common.IMAGE_PROMOTION_PATH + FileUploadConsulting.FileName;
                    }

                    var result = PromotionFactory.Insert(promotion);

                    if (result.PromotionID > 0)
                    {
                        Response.Redirect("ListPromotion.aspx", false);
                    }
                    else
                    {
                        lblErrorMessage.Visible = true;
                        lblErrorMessage.Text = Common.INSERT_FAIL;
                    }
                }

            }
            catch (Exception ex)
            {
                lblErrorMessage.Visible = true;
                lblErrorMessage.Text = Common.UPDATE_FAIL;
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }
    }
}

